package com.example.newland.application41;

import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private ImageView main_iv_bj;
    private ImageView main_iv_redgreen_switch;
    private ImageView main_iv_alarm_bj;
    private ImageView main_iv_alarm_switch;
    private SharedPreferences config;
    String token;
    private Integer m_xckgdls_integer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main_iv_bj = findViewById(R.id.main_iv_bj);
        main_iv_redgreen_switch = findViewById(R.id.main_iv_redgreen_switch);
        main_iv_alarm_bj = findViewById(R.id.main_iv_alarm_bj);
        main_iv_alarm_switch = findViewById(R.id.main_iv_alarm_switch);

        //子线程请求网络
        new Thread(){

            @Override
            public void run() {
                String login_url = "http://api.nlecloud.com/Users/Login";
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("Account","17309339946");
                    jsonObject.put("Password","17309339946");
                    jsonObject.put("IsRememberMe",true);

                    String s = jsonObject.toString();
                    String login_json = HttpUrl.post(login_url, s, null);
                    //解析登录获取的json
                    JSONObject jsonObject1 = new JSONObject(login_json);
                    int status = jsonObject1.getInt("Status");
                    if(status == 0){
                        JSONObject resultObj = jsonObject1.getJSONObject("ResultObj");
                        String accessToken = resultObj.getString("AccessToken");
                        Log.e("accessToken",accessToken);

                        //保存起来
                        config = getSharedPreferences("config", MODE_PRIVATE);
                        SharedPreferences.Editor edit = config.edit();
                        edit.putString(XindaluConfig.token, accessToken);
                        edit.commit();

                        //获取token
                        token = config.getString(XindaluConfig.token, "");
                    }else{
                        Log.e("err","登录状态错误");
                    }
                } catch (Exception e) {
                    Log.e("获取token错误",e.getMessage().toString());
                }
            }
        }.start();

        new Thread(){

            @Override
            public void run() {
               while(true){
                   String m_xckgdls_url = "http://api.nlecloud.com/devices/778003/Datas?deviceId=778003&ApiTags=m_xckgdls";
                   String m_xckgdls_json = HttpUrl.get(m_xckgdls_url, token);
                   Log.i("行程开关(单轮式)json",m_xckgdls_json);
                   //解析行程开关(单轮式)json
                   try {
                       JSONObject jsonObject = new JSONObject(m_xckgdls_json);
                       int status = jsonObject.getInt("Status");
                       if(status == 0){
                           JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                           JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                           JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                           JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                           JSONObject jsonObject2 = pointDTO.getJSONObject(0);
                           String value = jsonObject2.getString("Value");
                           Log.e("行程开关(单轮式)状态",value);

                           m_xckgdls_integer = Integer.valueOf(value);

                           Message message = new Message();
                           message.what = 1;
                           Bundle bundle = new Bundle();
                           bundle.putString("value",value);
                           message.setData(bundle);
                           handler.sendMessage(message);
                           if(m_xckgdls_integer == 0){
                               //绿灯状态,图片改变
                               String m_multi_green_url = "http://api.nlecloud.com/Cmds?deviceId=778003&apiTag=m_multi_green";
                               HttpUrl.post(m_multi_green_url,"1",token);
                               String m_multi_red_url = "http://api.nlecloud.com/Cmds?deviceId=778003&apiTag=m_multi_red";
                               HttpUrl.post(m_multi_red_url,"0",token);
                           }else{
                               //绿灯状态,图片改变
                               String m_multi_green_url = "http://api.nlecloud.com/Cmds?deviceId=778003&apiTag=m_multi_green";
                               HttpUrl.post(m_multi_green_url,"0",token);
                               String m_multi_red_url = "http://api.nlecloud.com/Cmds?deviceId=778003&apiTag=m_multi_red";
                               HttpUrl.post(m_multi_red_url,"1",token);
                           }
                       }else{
                           Log.e("err","行程开关(单轮式)状态错误");
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
                   try {
                       Thread.sleep(2000);
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                while(true){
                    String m_laser_url = "http://api.nlecloud.com/devices/778003/Datas?deviceId=778003&ApiTags=m_laser";
                    String m_laser_jaon = HttpUrl.get(m_laser_url, token);
                    Log.i("激光对射json",m_laser_jaon);
                    try {
                        JSONObject jsonObject = new JSONObject(m_laser_jaon);
                        int status = jsonObject.getInt("Status");
                        if(status == 0){
                            JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                            JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                            JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                            JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                            JSONObject jsonObject2 = pointDTO.getJSONObject(0);
                            String m_laser = jsonObject2.getString("Value");
                            Integer m_laser_int = Integer.valueOf(m_laser);
                            Log.e("激光对射模组状态",m_laser);



                            if((m_xckgdls_integer== 1 && m_laser_int == 0) || (m_xckgdls_integer== 0 && m_laser_int == 1)){
                                new Thread(){
                                    @Override
                                    public void run() {
                                        super.run();
                                        String m_rotating_lamp_url = "http://api.nlecloud.com/Cmds?deviceId=778003&apiTag=m_rotating_lamp";
                                        HttpUrl.post(m_rotating_lamp_url,"0",token);
                                    }
                                }.start();
                            }else if(m_laser_int == 1 && m_xckgdls_integer == 1){
                                new Thread(){
                                    @Override
                                    public void run() {
                                        super.run();
                                        String m_rotating_lamp_url = "http://api.nlecloud.com/Cmds?deviceId=778003&apiTag=m_rotating_lamp";
                                        HttpUrl.post(m_rotating_lamp_url,"1",token);
                                    }
                                }.start();
                            }



                            Message message = new Message();
                            message.what = 2;

                            Bundle bundle = new Bundle();
                            bundle.putString("m_laser",m_laser);
                            message.setData(bundle);

                            handler.sendMessage(message);
                        }else{
                            Log.e("err","激光对射状态错误");
                        }

                    } catch (Exception e) {
                        Log.e("err",e.getMessage().toString());
                    }

                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();


    }

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Bundle data = msg.getData();
                    String m_xckgdls = data.getString("value");
                    Integer m_xckgdls_int = Integer.valueOf(m_xckgdls);
                    if(m_xckgdls_integer == 0){
                        main_iv_redgreen_switch.setImageResource(R.drawable.switch_on);
                        main_iv_bj.setImageResource(R.mipmap.green_light);
                    }else{
                        main_iv_redgreen_switch.setImageResource(R.drawable.switch_off);
                    }
                    break;
                    case 2:
                        Bundle data1 = msg.getData();
                        String m_laser = data1.getString("m_laser");
                        Integer integer = Integer.valueOf(m_laser);
                        if(integer == 1 && m_xckgdls_integer == 1){
                            main_iv_bj.setImageResource(R.mipmap.redtwo);
                            main_iv_alarm_bj.setImageResource(R.mipmap.open);
                            main_iv_alarm_switch.setImageResource(R.mipmap.switch_on);
                        }else if (m_xckgdls_integer== 1 && integer == 0){
                            main_iv_bj.setImageResource(R.mipmap.redone);
                            main_iv_alarm_bj.setImageResource(R.mipmap.close_light);
                            main_iv_alarm_switch.setImageResource(R.mipmap.switch_off);
                        }else {
                            main_iv_alarm_bj.setImageResource(R.mipmap.close_light);
                            main_iv_alarm_switch.setImageResource(R.mipmap.switch_off);
                        }
                        break;
            }
        }
    };
}
